
<?php
    include_once('Configs/database.php');
     include_once('Models/LichThucHanh.php');
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        //$hocki = $_POST['cbohocki'];
        //$nienkhoa = $_POST['cbonienkhoa'];
        //$maso = $_POST['cbomaso'];
        $manhomth = trim($_POST['txtmanhomth']);
		$tuan = $_POST['cbotuan'];
       
        $thu = $_POST['cbothu'];
        $tietbd = $_POST['cbotietbd'];
        $sotiet = $_POST['cbosotiet'];
        $maphong = trim($_POST['txtmaphong']);
        $lichth = new LichThucHanh();
        if (isset($_POST['btnthemlichth'])){
           
           $lichth = new LichThucHanh();
            $lichth->setMaNhomTH($manhomth);
			$lichth->setTuan($tuan);
            $lichth->setThu($thu);
            $lichth->setTietBD($tietbd);
            $lichth->setSoTiet($sotiet);
            $lichth->setMaPhong($maphong);
			if ($lichth->isExits("LichThucHanh","manhomth='".$lichth->getMaNhomTH()."' and tuan=".$lichth->getTuan()." and thu=".$lichth->getThu()."") > 0){
                echo "<script language=javascript> alert('Không thêm được do lịch thực hành này đã tồn tại!'); </script>";
                //require('Views/Capnhatnhomth.php');
			}
            else{
                       
                $query = "select maphong from LichThucHanh a, LopHocPhan b,NhomThucHanh c ";			
                $query .= "where a.manhomth = c.manhomth and b.malophp=c.malophp and  hocki='".$hocki."' and namhoc='".$namhoc."' and maphong='".$maphong."' and  (tuan = ".$tuan." and thu=".$thu."  and ";
                $query .= "(((".$tietbd."+".$sotiet."-1)>=tietbd and (tietbd+sotiet-1)>=(".$tietbd."+".$sotiet."-1))or (".$tietbd.">=tietbd and (tietbd+sotiet-1)>=".$tietbd.") or ((tietbd+sotiet-1)=".$tietbd.") or (tietbd=(".$tietbd."+".$sotiet."-1))))";
				
                $lichth->setQuery($query);
                $resultphong = $lichth->fetchAll();
                $rows = mysql_fetch_array($resultphong);
                if ($rows['maphong'] != ""){
                   echo "<script language=javascript> alert('Không thêm được do trùng phòng máy!'); </script>";
                    //require('Views/Capnhatnhomth.php');
                }
                else{
                    if ($lichth->themLichThucHanh() > 0){
                        echo "<script language=javascript> alert('Đã thêm thành công!'); </script>";
                        //require('Views/Capnhatnhomth.php');
                    }
                    else{
                        echo "<script language=javascript> alert('Không thêm được!'); </script>";
                        //require('Views/Capnhatnhomth.php');
                    }
                }
                 
            }
           
            
            
        }
		if (isset($_POST['btnsualichth'])){
           
           $lichth = new LichThucHanh();
            $lichth->setMaNhomTH($manhomth);
			$lichth->setTuan($tuan);
            $lichth->setThu($thu);
            $lichth->setTietBD($tietbd);
            $lichth->setSoTiet($sotiet);
            $lichth->setMaPhong($maphong);
			/*if ($lichth->isExits("LichThucHanh","manhomth='".$lichth->getMaNhomTH()."' and tuan=".$lichth->getTuan()." and thu=".$lichth->getThu()."") > 0){
                echo "<script language=javascript> alert('Không thêm được do lịch thực hành này đã tồn tại!'); </script>";
                //require('Views/Capnhatnhomth.php');
			}
            else{*/
                       
                /*$query = "select maphong from LichThucHanh a, LopHocPhan b,NhomThucHanh c ";			
                $query .= "where a.manhomth = c.manhomth and b.malophp=c.malophp and  hocki='".$hocki."' and namhoc='".$namhoc."' and maphong='".$maphong."' and  (tuan = ".$tuan." and thu=".$thu."  and ";
                $query .= "(((".$tietbd."+".$sotiet."-1)>=tietbd and (tietbd+sotiet-1)>=(".$tietbd."+".$sotiet."-1))or (".$tietbd.">=tietbd and (tietbd+sotiet-1)>=".$tietbd.") or ((tietbd+sotiet-1)=".$tietbd.") or (tietbd=(".$tietbd."+".$sotiet."-1))))";
				
                $lichth->setQuery($query);
                $resultphong = $lichth->fetchAll();
                $rows = mysql_fetch_array($resultphong);
                if ($rows['maphong'] != ""){
                   echo "<script language=javascript> alert('Không sửa được do trùng phòng máy!'); </script>";
                    //require('Views/Capnhatnhomth.php');
                }
                else{*/
                    if ($lichth->suaLichThucHanh() > 0){
                        echo "<script language=javascript> alert('Đã sửa thành công!'); </script>";
                        //require('Views/Capnhatnhomth.php');
                    }
                    else{
                        echo "<script language=javascript> alert('Không sửa được!'); </script>";
                        //require('Views/Capnhatnhomth.php');
                    }
                //}
                 
            //}
           
            
            
        }
        if (isset($_POST['btnxoalichth'])){
            $lichth = new LichThucHanh();
			 $lichth->setMaNhomTH($manhomth);
           	$lichth->setTuan($tuan);
            $lichth->setThu($thu);
            $lichth->setTietBD($tietbd);
            //$lichth->setSoTiet($sotiet);
            if ($lichth->xoaLichThucHanh() > 0){
                echo "<script language=javascript> alert('Đã xóa thành công!'); </script>";
               
            }
            else{
				
                echo "<script language=javascript> alert('Không xóa được!'); </script>";
                
            } 
        }
        if (isset($_POST['btnxem'])){
            $namhoc = $_POST['cbonamhoc'];
            $hocki = $_POST['cbohocki'];
            $maquyen = $_SESSION['nmaquyen'];
            $maso = " ";
            if ($maquyen == 'GV'){
                $maso = $_SESSION['nmaso'];
            }
            else if ($maquyen == 'AD'){
                $maso = $_POST['txtmaso'];
            }
            else{
                $maso = $_POST['cbomaso'];
            }
            $db->setQuery("select concat(holot,' ', ten) as hoten from CanBo where maso='".$maso."'");
            $result = $db->fetchAll();
            $rows = mysql_fetch_array($result);
            $hoten = $rows['hoten'];
            $query1 = "select distinct thu,a.mahp,kihieu,tenhp,a.malophp,siso,tietbd, sotiet, maso, maphong,a.tuanhoc ";
            $query1 .= "from LopHocPhan a, LichThucHanh b, HocPhan c ";
            $query1 .= "where maso='".$maso."' and hocki=".$hocki." and namhoc='".$namhoc."' ";
            $query1 .= "and a.malophp = b.malophp and a.mahp = c.mahp ";
            $query1 .= "order by thu,tietbd ";
            $lichth->setQuery($query1);
            $result1 = $lichth->fetchAll();
            $query2 = "select distinct a.malophp,kihieu,a.mahp,tenhp,siso,a.tuanhoc ";
            $query2 .= "from LopHocPhan a, HocPhan b ";
            $query2 .= "where maso='".$maso."' and hocki=".$hocki." and namhoc='".$namhoc."'  and (manhom =5 or manhom =6) ";
            $query2 .= "and a.mahp = b.mahp ";
            $query2 .= "order by a.mahp ";
            $lichth->setQuery($query2);
            //echo $lichth->getQuery();
            $result2 = $lichth->fetchAll();
            require('Views/xemLichThucHanh.php');
        }
    }
    else{
        if ($_GET['option']=='xemLichThucHanh'){
            require('Views/xemLichThucHanh.php');
			//require('Views/LichThucHanh.php');
        }
        if ($_GET['option']=='capnhatlichth'){
            require('Views/CapnhatLichThucHanh.php');
        }
        
    }
  
                                
  
?>
